const formatTime = date => {
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()

    return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
    n = n.toString()
    return n[1] ? n : '0' + n
}


// 返回本天00: 00
const getFirstTimeInThisDay = date => {

    let t = date.valueOf()
    let day = date.getDay()
    day = day === 0 ? 7 : day
    t -= ((date.getHours() * 60 + date.getMinutes()) * 60 + date.getSeconds()) * 1000 + date.getMilliseconds()
    return t
}


// 返回本周第一天00: 00
const getFirstTimeInThisWeek = date => {

    let t = date.valueOf()
    let day = date.getDay()
    day = day === 0 ? 7 : day
    t -= ((((day - 1) * 24 + date.getHours()) * 60 + date.getMinutes()) * 60 + date.getSeconds()) * 1000 + date.getMilliseconds()
    return t
}


// 仿微信聊天时间格式化
const formatTime2 = date => {

    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()
    const week = date.getDay()

    const now = new Date()

    // 早中晚凌晨
    const getRoughTimeInOneDay = hour => {
        if (hour < 6) return '凌晨'
        else if (hour < 11) return '早上'
        else if (hour < 13) return '中午'
        else if (hour < 18) return '下午'
        else return '晚上'
    }
    const formatTime = (hour, minute) => {
        return `${hour < 10 ? 0 : ''}${hour}:${minute < 10 ? 0 : ''}${minute}`
    }
    // 返回是否是同一周
    const isSameWeek = (d1, d2) => {

        return getFirstTimeInThisWeek(d1) === getFirstTimeInThisWeek(d2)
    }
    // 返回是否是同一天
    const isSameDay = (d1, d2) => getFirstTimeInThisDay(d1) === getFirstTimeInThisDay(d2)
    // 返回是否d1在d2前一天
    const isTheDayBefore = (d1, d2) => getFirstTimeInThisDay(d1) === getFirstTimeInThisDay(d2) - 24 * 60 * 60 * 1000


    if (year < now.getFullYear()) {
        return `${year}年${month}月${day}日 ${getRoughTimeInOneDay(hour)}${formatTime(hour,minute)}`
    } else {
        // 今年

        if (isSameDay(date, now)) {
            // 今天
            return `${getRoughTimeInOneDay(hour)}${formatTime(hour, minute)}`
        } else if (isTheDayBefore(date, now)) {
            // 昨天
            return `昨天 ${getRoughTimeInOneDay(hour)}${formatTime(hour, minute)}`
        } else if (isSameWeek(now, date)) {
            // 本周
            let day = date.getDay()
            day = day === 0 ? 7 : day
            day--
            let dayS = ['一', '二', '三', '四', '五', '六', '日']
            return `周${dayS[day]} ${getRoughTimeInOneDay(hour)}${formatTime(hour, minute)}`
        } else {
            return `${month}月${day}日 ${getRoughTimeInOneDay(hour)}${formatTime(hour, minute)}`
        }
    }
}

const throttle = (func,interval)=>{
	let last = 0
	return function(...args){
		if(Date.now()-last>interval){
			func.apply(this,args)
			last = Date.now()
		}
	}
}

module.exports = {
    formatTime: formatTime,
    formatTime2: formatTime2,
	throttle
}